﻿using System;
using System.Configuration;

namespace SPDeployer.Core.Config
{
    public class ListConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty(Definitions.Lists.NAME, IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return this[Definitions.Lists.NAME].ToString(); }
            set { this[Definitions.Lists.NAME] = value; }
        }

        [ConfigurationProperty(Definitions.Lists.TYPE, IsRequired = false, DefaultValue = 101)]
        public int Type
        {
            get { return Int32.Parse(this[Definitions.Lists.TYPE].ToString()); }
            set { this[Definitions.Lists.TYPE] = value; }
        }

        [ConfigurationProperty(Definitions.CONTENT_TYPES_COLLECTION, IsRequired = false)]
        public ContentTypeConfigurationElementCollection ContentTypes
        {
            get { return (ContentTypeConfigurationElementCollection)this[Definitions.CONTENT_TYPES_COLLECTION]; }
            set { this[Definitions.CONTENT_TYPES_COLLECTION] = value; }
        }
    }

    [ConfigurationCollection(typeof(ListConfigurationElement), AddItemName = "List")]
    public class ListConfigurationElementCollection : ConfigurationElementCollection
    {
        public ListConfigurationElement this[int index]
        {
            get { return BaseGet(index) as ListConfigurationElement; }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ListConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ListConfigurationElement)element).Name;
        }
    }
}